from flask import Blueprint, render_template, request, jsonify
from flask_security import current_user

from application.controller.back_handler import upload_getter, add_article

back_bp = Blueprint('back', __name__, url_prefix='/back')


@back_bp.route('/editor')
def back_editor():
    return render_template('back/editor.html')


@back_bp.route('/back')
def back_page():
    return render_template('back/base.html')


@back_bp.route('/get_text', methods=['POST'])
def get_text():
    print(request.form)
    data = dict(request.form)
    data['author_id'] = current_user.id
    add_article(data)
    return 'suc'


@back_bp.route('/fileUploadUrl', methods=['POST'])
def fileupload():
    if request.method == 'POST' and 'upload' in request.files:
        res = upload_getter(request.files['upload'], 'fileupload')
        return jsonify(res)
    else:
        return jsonify({'uploaded': 0})


@back_bp.route('/imageUploadUrl', methods=['POST'])
def imageupload():
    if request.method == 'POST' and 'upload' in request.files:
        res = upload_getter(request.files['upload'], 'imageupload')
        return jsonify(res)
    else:
        return jsonify({'uploaded': 0})
